
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <cstring>
using namespace std;

#define PATH "./fifo"
#define MODE 0666

class Fifo
{
public:
    Fifo (const string& path = PATH): _path(path)
    {
        int ret = mkfifo(_path.c_str(), MODE); //创建一个命名管道（以path为名字），成功返回0，失败返回-1
        if(ret == -1)
            cerr << "namedpipe failed, error code: " << errno << ", error message: " << strerror(errno) << endl;
        else
            cout << "namedpipe success!" << endl;
    }

    ~Fifo()
    {
        int ret = unlink(_path.c_str()); //释放命名管道，成功返回0，失败返回-1
        if(ret == -1)
            cerr << "namedpipe failed, error code: " << errno << ", error message: " << strerror(errno) << endl;
        else
            cout << "namedpipe success!" << endl;
    }

private:
    string _path; //文件路径+文件名
};